<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="设计师昵称" prop="designerName">
        <el-input v-model="queryParams.designerName" placeholder="请输入设计师昵称" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="授权手机号" prop="tel">
        <el-input v-model="queryParams.tel" placeholder="请输入授权手机号" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery" size="small">重置</el-button>
        <el-button type="success" plain icon="Plus" size="small" @click="handleAdd"
          v-hasPermi="['decor:designer:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="designerList" row-key="designerId" default-expand-all :tree-props="{ children: 'updateRecords' }">
      <el-table-column label="ID" align="center" :show-overflow-tooltip="true" prop="designerId" />
      <el-table-column label="设计师昵称" align="center" :show-overflow-tooltip="true" prop="designerName" />
      <el-table-column label="授权手机号" align="center" :show-overflow-tooltip="true" prop="tel" />
      <el-table-column label="头像" align="center" prop="url">
        <template #default="scope">
          <ImagePreview :width="60" :height="60" :src="scope.row.url" :preview-src-list="[scope.row.url]" />
        </template>
      </el-table-column>
      <el-table-column label="职位" align="center" :show-overflow-tooltip="true" prop="position"></el-table-column>
      <el-table-column label="机构" align="center" :show-overflow-tooltip="true" prop="company" />
      <el-table-column label="工作年限" align="center" :show-overflow-tooltip="true" prop="workYears" />
      <el-table-column label="设计师状态" :show-overflow-tooltip="true" align="center" prop="designerStatus">
        <template #default="scope">
          <dict-tag :options="designer_status" :value="scope.row.designerStatus" />
        </template>
      </el-table-column>
      <el-table-column label="排序" :show-overflow-tooltip="true" align="center" prop="sort1">
        <template #default="scope">
          <div>{{ scope.row.sort1 }}</div>
          <el-button link type="warning" size="small" @click="editSort(scope.row)"
            v-hasPermi="['decor:designer:edit']">修改排序</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" size="small" @click="handleUpdate(scope.row)"
            v-hasPermi="['decor:designer:edit']">修改</el-button>
          <el-button link type="warning" icon="Delete" size="small" @click="handleDelete(scope.row)"
            v-hasPermi="['decor:designer:remove']">删除</el-button>
          <el-button link type="primary"
            v-show="scope.row.designerStatus === 1 || scope.row.designerStatus === 3"
            :icon="scope.row.designerStatus === 3 ? 'Upload' : 'Download'" size="small" @click="handleDownload(scope.row)"
            v-hasPermi="['decor:designer:edit']">{{ scope.row.designerStatus === 3 ?
              '上架' : '下架' }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script setup>
import { listDesigner, delDesigner, getImgUrl, updateDesigner } from "@/api/decor/designer";
const { proxy } = getCurrentInstance();
const { designer_status } = proxy.useDict('designer_status');
const router = useRouter();
// 表格数据
const designerList = ref([]);
// 遮罩层
const loading = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    designerName: null,
    tel: null,
    // orderByColumn: 'designerId',
    // isAsc: 'desc'
  },
});
const { queryParams } = toRefs(data);

// 查询设计师信息列表
function getList() {
  loading.value = true;
  listDesigner(queryParams.value).then(response => {
    let imgUrlPromises = [];
    let data = response.rows.map((v) => {
      return { ...v, url: '' }
    })
    data.forEach((v) => {
      // 每次循环创建一个 Promise，并将其推入数组
      if (v.avatarId) {
        let promise = getImgUrl(v.avatarId).then((res) => {
          v.url = res.data[0].url;
        });
        imgUrlPromises.push(promise);
      }
      if (v.updateRecords.length > 0) {
        if (v.updateRecords[0].avatarId) {
          let promiseUpdateRecords = getImgUrl(v.updateRecords[0].avatarId).then((res) => {
            v.updateRecords[0].url = res.data[0].url;
          });
          imgUrlPromises.push(promiseUpdateRecords);
        }
      }
    })
    Promise.all(imgUrlPromises).then(() => {
      // 所有的异步操作完成后再更新 designerList.value
      designerList.value = data;
      total.value = response.total;
      loading.value = false;
    });

  });
}

// 搜索按钮操作
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

// 重置按钮操作
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 新增按钮操作
const handleAdd = () => {
  router.push("/millwright/editMillwright");
}
// 修改按钮操作
const handleUpdate = (row) => {
  router.push("/millwright/editMillwright/" + row.designerId);
}
// 调整排序
const editSort = (row) => {
  proxy.$prompt('请输入"' + row.designerName + '"的排序', "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    closeOnClickModal: false,
    inputPattern: /^(?:[1-9][0-9]{0,3})$/,
    inputErrorMessage: "请输入介于 1 和 9999 之间的数字",
  }).then(({ value }) => {
    const data = {
      ...row,
      sort1: value
    }
    updateDesigner(data).then(response => {
      proxy.$modal.msgSuccess("修改排序成功");
      getList()
    });
  }).catch(() => { });
}
// 删除按钮操作
const handleDelete = (row) => {
  proxy.$modal.confirm('是否确认删除设计师昵称为"' + row.designerName + '"的数据？').then(function () {
    return delDesigner(row.designerId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}
// 下架操作
const handleDownload = (row) => {
  const status = row.designerStatus === 3 ? '上架' : '下架'
  const designerStatus = row.designerStatus === 3 ? 1 : 3
  proxy.$modal.confirm(`是否确认${status}设计师"' ${row.designerName}  '"？`).then(function () {
    return updateDesigner({ ...row, designerStatus: designerStatus });
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess(`${status}成功`);
  }).catch(() => { });
}
getList();
</script>
